﻿using MediatR;
using SheepCultivation.Common.Domain.main;
using SheepCultivation.Common.Domain.SheepBasicInformation;
using SheepCultivation.Common.Domain.Week2;
using SheepCultivation.Common.Dto;
using SheepCultivation.Common.Infrastructure.Interface;
using SheepCultivation.Enum;
using System.Collections.Generic;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace SheepCultivation.API.AddliCation.BreedingD
{
    public class AppearancesSeachHandler : IRequestHandler<AppearancesSeachCommon, Result>
    {
        #region 仓储
        /// <summary>
        /// 品种仓储
        /// </summary>
        public readonly IBaseRepository<Sheepvariety> SheepvarietyRepository;
        /// <summary>
        /// 羊只称重仓储
        /// </summary>
        public readonly IBaseRepository<SheepWeighed> SheepWeighedRepository;
        /// <summary>
        /// 耳号仓储
        /// </summary>
        public readonly IBaseRepository<BasicInformation> BasicInformationRepository;
        /// <summary>
        /// 栋舍仓储
        /// </summary>
        public readonly IBaseRepository<Building> BuildingRepository;
        /// <summary>
        /// 栋舍类型仓储
        /// </summary> 
        public readonly IBaseRepository<BuildingType> BuildingTypeRepository;
        /// <summary>
        /// 栏位仓储
        /// </summary>
        public readonly IBaseRepository<Columns> ColumnsRepository;
        /// <summary>
        /// 品相测定仓储
        /// </summary>
        public readonly IBaseRepository<Appearances> AppearancesRepository;
        /// <summary>
        /// 首次妊检提示仓储
        /// </summary>
        public readonly IBaseRepository<Pregnancy> PregnancyRepository;
        /// <summary>
        /// 日志
        /// </summary>
        public readonly ILogger<AppearancesSeachHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="sheepvarietyRepository">品种</param>
        /// <param name="SheepWeighedRepository">羊只称重</param>
        /// <param name="BasicInformationRepository"></param>
        /// <param name="BuildingRepository">耳号</param>
        /// <param name="BuildingTypeRepository">栋舍</param>
        /// <param name="ColumnsRepository">栋舍类型</param>
        /// <param name="BodyRepository">体尺测定</param>
        /// <param name="logger">日志</param>
        public AppearancesSeachHandler(IBaseRepository<Sheepvariety> sheepvarietyRepository, IBaseRepository<SheepWeighed> SheepWeighedRepository, IBaseRepository<BasicInformation> BasicInformationRepository, IBaseRepository<Building> BuildingRepository, IBaseRepository<BuildingType> BuildingTypeRepository, IBaseRepository<Columns> ColumnsRepository, IBaseRepository<Appearances> AppearancesRepository, IBaseRepository<Pregnancy> PregnancyRepository, ILogger<AppearancesSeachHandler> logger)
        {
            this.SheepvarietyRepository = sheepvarietyRepository;
            this.SheepWeighedRepository = SheepWeighedRepository;
            this.BasicInformationRepository = BasicInformationRepository;
            this.BuildingRepository = BuildingRepository;
            this.BuildingTypeRepository = BuildingTypeRepository;
            this.ColumnsRepository = ColumnsRepository;
            this.AppearancesRepository = AppearancesRepository;
            this.PregnancyRepository = PregnancyRepository;
            this.logger = logger;
        }
        #endregion

        #region 品相测定显示
        /// <summary>
        /// 品相测定显示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(AppearancesSeachCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonEnumCode.error };
            //异常处理
            try
            {
                logger.LogInformation("品相测定显示成功!");
                var data = new pagessDto() { };
                var list = (from a in AppearancesRepository.GetAll()
                            join b in BasicInformationRepository.GetAll() on a.BreedingSheepArchiveId equals b.BreedingSheepArchiveId
                            join c in PregnancyRepository.GetAll() on a.BreedingSheepArchiveId equals c.BreedingSheepArchiveId
                            join d in ColumnsRepository.GetAll() on c.ColumnsId equals d.ColumnsId
                            join e in BuildingRepository.GetAll() on c.BuildingId equals e.BuildingId
                            join f in SheepvarietyRepository.GetAll() on b.BreedingSheepArchiveId equals f.SheepvarietyId
                            select new AppearancesDto()
                            {
                                AppearanceId = a.AppearanceId,
                                AppearancesDate = a.AppearancesDate,
                                Teeth = a.Teeth,
                                AppearancesHair = a.AppearancesHair,
                                AppearancesPigmentation = a.AppearancesPigmentation,
                                AppearancesHoofColor = a.AppearancesHoofColor,
                                AppearancesMouthColor = a.AppearancesMouthColor,
                                Horns = a.Horns,
                                Distribution = a.Distribution,
                                SheepSex = b.SheepSex,
                                BreedingSheepArchiveId = b.BreedingSheepArchiveId,
                                Auricle = b.Auricle,
                                PregnancyId = c.PregnancyId,
                                ColumnsId = d.ColumnsId,
                                ColumnsName = d.ColumnsName,
                                BuildingId = e.BuildingId,
                                BuildingName = e.BuildingName,
                                SheepvarietyId = f.SheepvarietyId,
                                SheepvarietyName = f.SheepvarietyName,
                            }).ToList();

                //查询条件：耳号 品种 性别
                if (!string.IsNullOrEmpty(request.auricle))
                {
                    list = list.Where(x => x.Auricle.Contains(request.auricle)).ToList();
                }
                if (request.sheepvarietyId > 0)
                {
                    list = list.Where(x => x.SheepvarietyId == request.sheepvarietyId).ToList();
                }
                if (request.sheepSex > 0)
                {
                    list = list.Where(x => x.SheepSex == request.sheepSex).ToList();
                }

                //分页
                data.totalCount = list.Count();
                data.pageCount = (int)((list.Count() * 1.0) / request.pageSize);
                data.data = list.OrderBy(x => x.AppearanceId).Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();

                result.Code = (int)CommonEnumCode.success;
                result.Data = data;
                result.Msg = "品相测定显示成功";
            }
            catch (Exception ex)
            {
                logger.LogError("品相测定显示失败!" + ex.Message);
                result.Msg = "品相测定显示失败";
            }
            return result;
        }
        #endregion
    }

    /// <summary>
    /// 分页
    /// </summary>
    public class pagessDto
    {
        public int totalCount { get; set; }
        public int pageCount { get; set; }
        public List<AppearancesDto> data { get; set; }
    }

    /// <summary>
    /// 品相测定Dto
    /// </summary>
    public class AppearancesDto()
    {
        /// <summary>
        /// 品相测定Id
        /// </summary>
        public int AppearanceId { get; set; }
        /// <summary>
        /// 测定日期
        /// </summary>
        public DateTime AppearancesDate { get; set; }
        /// <summary>
        /// 测定日龄
        /// </summary>
        public int AppearancesAge { get; set; }
        /// <summary>
        /// 牙齿咬合
        /// </summary>
        public int Teeth { get; set; }
        /// <summary>
        /// 毛发
        /// </summary>
        public string? AppearancesHair { get; set; }
        /// <summary>
        /// 色斑
        /// </summary>
        public string? AppearancesPigmentation { get; set; }
        /// <summary>
        /// 蹄颜色
        /// </summary>
        public string? AppearancesHoofColor { get; set; }
        /// <summary>
        /// 蹄颜色
        /// </summary>
        public string? AppearancesMouthColor { get; set; }
        /// <summary>
        /// 角
        /// </summary>
        public int Horns { get; set; }
        /// <summary>
        /// 脂肪分布
        /// </summary>
        public int Distribution { get; set; }
        /// <summary>
        /// 栏位Id
        /// </summary>
        public int ColumnsId { get; set; }
        /// <summary>
        /// 栋舍Id
        /// </summary>
        public int BuildingId { get; set; }
        /// <summary>
        /// 栏位名称
        /// </summary>
        public string? ColumnsName { get; set; }
        /// <summary>
        /// 栋舍名称
        /// </summary>
        public string? BuildingName { get; set; }
        /// <summary>
        /// 品种Id
        /// </summary>
        public int SheepvarietyId { get; set; }
        /// <summary>
        /// 品种名称
        /// </summary>
        public string? SheepvarietyName { get; set; }
        public int BreedingSheepArchiveId { get; set; }
        /// <summary> 
        /// 耳号
        /// </summary>
        public string Auricle { get; set; }
        public int SheepSex { get; set; }
        public int PregnancyId { get; set; }
    };
}
